package com.ruoyi.web.controller.wx.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.HomeCollect;
import com.ruoyi.system.domain.HomeHistory;
import com.ruoyi.system.domain.HomeUser;
import com.ruoyi.system.domain.SysListingInformation;
import com.ruoyi.system.domain.vo.HomeUserVO;
import com.ruoyi.system.domain.vo.HomeVO;
import com.ruoyi.system.domain.vo.SysListingInformationListVO;
import com.ruoyi.system.mapper.HomeCollectMapper;
import com.ruoyi.system.mapper.HomeHistoryMapper;
import com.ruoyi.system.mapper.HomeUserMapper;
import com.ruoyi.system.mapper.SysListingInformationMapper;
import com.ruoyi.web.controller.wx.service.IWxUserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WxUserServiceImpl implements IWxUserService {

    @Autowired
    private HomeUserMapper homeUserMapper;

    @Autowired
    private HomeHistoryMapper homeHistoryMapper;

    @Autowired
    private SysListingInformationMapper sysListingInformationMapper;

    @Autowired
    private HomeCollectMapper homeCollectMapper;

    @Override
    public HomeUser userDetail(Long id) {
        HomeUser homeUser = homeUserMapper.selectById(id);
        return homeUser;
    }

    @Override
    public void updateDetail(HomeUserVO homeUserVO) {
        HomeUser homeUser = homeUserMapper.selectById(homeUserVO.getId());
        homeUser.setHeadPortrait(homeUserVO.getHeadPortrait());
        homeUser.setPhoneNumber(homeUserVO.getPhoneNumber());
        homeUser.setUserName(homeUserVO.getUserName());
        homeUserMapper.updateById(homeUser);
    }

    @Override
    public List<SysListingInformationListVO> userHistory(Long id) {
        LambdaQueryWrapper<HomeHistory> homeHistoryLambdaQueryWrapper=new LambdaQueryWrapper<>();
        homeHistoryLambdaQueryWrapper.eq(HomeHistory::getUserId,id);
        homeHistoryLambdaQueryWrapper.orderByDesc(HomeHistory::getCreateTime);
        List<HomeHistory> homeHistories = homeHistoryMapper.selectList(homeHistoryLambdaQueryWrapper);

        List<Long> homeIdList = homeHistories.stream().map(HomeHistory::getHomeId).collect(Collectors.toList());

        LambdaQueryWrapper<SysListingInformation> sysListingInformationLambdaQueryWrapper=new LambdaQueryWrapper<>();
        sysListingInformationLambdaQueryWrapper.in(SysListingInformation::getId,homeIdList);
        sysListingInformationLambdaQueryWrapper.eq(SysListingInformation::getHomeStatus,0);
        List<SysListingInformation> sysListingInformations = sysListingInformationMapper.selectList(sysListingInformationLambdaQueryWrapper);
        List<SysListingInformationListVO> listVO = sysListingInformations.stream().map(x -> {
            SysListingInformationListVO vo = new SysListingInformationListVO();
            BeanUtils.copyProperties(x, vo);
            if (ObjectUtils.isNotEmpty(x.getImages())) {
                List<String> imageList = JSON.parseArray(x.getImages(), String.class);
                vo.setImage(imageList.get(0));
            }
            return vo;
        }).collect(Collectors.toList());
        return listVO;
    }

    @Override
    public void userAttention(HomeVO homeVO) {
        LambdaQueryWrapper<HomeCollect> homeCollectLambdaQueryWrapper=new LambdaQueryWrapper<>();
        homeCollectLambdaQueryWrapper.eq(HomeCollect::getUserId,homeVO.getUserId());
        homeCollectLambdaQueryWrapper.eq(HomeCollect::getHomeId,homeVO.getListingId());
        HomeCollect homeCollect = homeCollectMapper.selectOne(homeCollectLambdaQueryWrapper);
        if (ObjectUtils.isNotEmpty(homeCollect)){
            homeCollectMapper.deleteById(homeCollect);
        }else {
            homeCollect=new HomeCollect();
            homeCollect.setUserId(homeVO.getUserId());
            homeCollect.setHomeId(homeVO.getListingId());
            homeCollect.setCreateTime(new Date());
            homeCollectMapper.insert(homeCollect);
        }
    }

    @Override
    public List<SysListingInformationListVO> attentionList(Long id) {
        LambdaQueryWrapper<HomeCollect> homeCollectLambdaQueryWrapper=new LambdaQueryWrapper<>();
        homeCollectLambdaQueryWrapper.eq(HomeCollect::getUserId,id);
        homeCollectLambdaQueryWrapper.orderByDesc(HomeCollect::getCreateTime);
        List<HomeCollect> homeCollects = homeCollectMapper.selectList(homeCollectLambdaQueryWrapper);

        List<Long> homeIdList = homeCollects.stream().map(HomeCollect::getHomeId).collect(Collectors.toList());

        LambdaQueryWrapper<SysListingInformation> sysListingInformationLambdaQueryWrapper=new LambdaQueryWrapper<>();
        sysListingInformationLambdaQueryWrapper.in(SysListingInformation::getId,homeIdList);
        sysListingInformationLambdaQueryWrapper.eq(SysListingInformation::getHomeStatus,0);
        List<SysListingInformation> sysListingInformations = sysListingInformationMapper.selectList(sysListingInformationLambdaQueryWrapper);
        List<SysListingInformationListVO> listVO = sysListingInformations.stream().map(x -> {
            SysListingInformationListVO vo = new SysListingInformationListVO();
            BeanUtils.copyProperties(x, vo);
            if (ObjectUtils.isNotEmpty(x.getImages())) {
                List<String> imageList = JSON.parseArray(x.getImages(), String.class);
                vo.setImage(imageList.get(0));
            }
            return vo;
        }).collect(Collectors.toList());
        return listVO;
    }


    @Override
    public List<SysListingInformationListVO> myHome(Long id) {
        LambdaQueryWrapper<SysListingInformation> sysListingInformationLambdaQueryWrapper=new LambdaQueryWrapper<>();
        sysListingInformationLambdaQueryWrapper.eq(SysListingInformation::getHomeStatus,0);
        sysListingInformationLambdaQueryWrapper.eq(SysListingInformation::getHomeUserId,id);
        sysListingInformationLambdaQueryWrapper.orderByDesc(SysListingInformation::getCreateTime);
        List<SysListingInformation> sysListingInformationList = sysListingInformationMapper.selectList(sysListingInformationLambdaQueryWrapper);
        List<SysListingInformationListVO> listVO = sysListingInformationList.stream().map(x -> {
            SysListingInformationListVO vo = new SysListingInformationListVO();
            BeanUtils.copyProperties(x, vo);
            if (ObjectUtils.isNotEmpty(x.getImages())) {
                List<String> imageList = JSON.parseArray(x.getImages(), String.class);
                vo.setImage(imageList.get(0));
            }
            return vo;
        }).collect(Collectors.toList());
        return listVO;
    }

    @Override
    public void updateHome(HomeVO homeVO) {
        for (Long homeId : homeVO.getHomeIdList()) {
            SysListingInformation sysListingInformation = sysListingInformationMapper.selectById(homeId);
            if (sysListingInformation.getHomeStatus()==0){
                sysListingInformation.setHomeStatus(1);
                sysListingInformationMapper.updateById(sysListingInformation);
            }
        }
    }

    @Override
    public void deleteHome(HomeVO homeVO) {
        List<Long> homeIdList = homeVO.getHomeIdList();
        if (homeIdList.size()>0){
            sysListingInformationMapper.deleteBatchIds(homeIdList);
        }
    }

    @Override
    public void deleteAttention(HomeVO homeVO) {
        LambdaQueryWrapper<HomeCollect> homeCollectLambdaQueryWrapper=new LambdaQueryWrapper<>();
        homeCollectLambdaQueryWrapper.eq(HomeCollect::getUserId,homeVO.getUserId());
        if (homeVO.getHomeIdList().size()>0) {
            homeCollectLambdaQueryWrapper.in(HomeCollect::getHomeId,homeVO.getHomeIdList());
            homeCollectMapper.delete(homeCollectLambdaQueryWrapper);
        }
    }
}
